package service.impl.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import service.impl.BaseDataServiceImpl;
import service.interf.security.IAuthority;

import common.impl.exception.ServiceException;

import dao.interf.IAuthorityDAO;
import dao.interf.IGroupAuthorityDAO;
import dao.interf.IGroupDAO;
import dao.interf.IGroupMemberDAO;
import dao.interf.IUserDAO;
import dao.pojo.Authority;
import dao.pojo.Group;
import dao.pojo.GroupAuthority;
import dao.pojo.GroupMember;
import dao.pojo.User;

/**
 *  Title:AuthorityImpl 鉴权实现类
 *  Description:提供鉴权相关接口
 *  @author yangl
 *  @date 2012-11-30
 *  @version 1.0
 */
public class AuthorityImpl extends BaseDataServiceImpl  implements IAuthority {
	
	/**安全组接口*/
	private IGroupDAO groupDao;
	
	/**安全权限接口*/
	private IAuthorityDAO authDao;
	
	/**安全组成员接口*/
	private IGroupMemberDAO gmDao;
	
	/**安全组权限接口*/
	private IGroupAuthorityDAO gaDao;
	
	/**安全用户接口*/
	private IUserDAO userDao;
	
	/**日志*/
	private final Log log = LogFactory.getLog(getClass());

	/************************** dao setter ************************/
	
	/**
	 * Title:setGroupDao
	 * Description:为Spring框架注入IGroupDAO提供方法
	 * @param groupDao 传入GroupDAO
	 */
	public void setGroupDao(IGroupDAO groupDao) {
		this.groupDao = groupDao;
	}

	/**
	 * Title:setAuthDao
	 * Description:为Spring框架注入IAuthorityDAO提供方法
	 * @param authDao 传入AuthorityDAO
	 */
	public void setAuthDao(IAuthorityDAO authDao) {
		this.authDao = authDao;
	}

	/**
	 * Title:setGmDao
	 * Description:为Spring框架注入IGroupMemberDAO提供方法
	 * @param gmDao 传入GroupMemberDAO
	 */
	public void setGmDao(IGroupMemberDAO gmDao) {
		this.gmDao = gmDao;
	}

	/**
	 * Title:setGaDao
	 * Description:为Spring框架注入IGroupAuthorityDAO提供方法
	 * @param gaDao 传入GroupAuthorityDAO
	 */
	public void setGaDao(IGroupAuthorityDAO gaDao) {
		this.gaDao = gaDao;
	}

	/**
	 * Title:setGroupDao
	 * Description:为Spring框架注入IGroupDAO提供方法
	 * @param userDao 传入UserDAO
	 */
	public void setUserDao(IUserDAO userDao) {
		this.userDao = userDao;
	}
	
	/************************* 用户组 *******************************/
	/**
	 * Title:addGroup
	 * Description:添加组
	 * @param model Group对象
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addGroup(Group model)throws Exception{
		Group group = new Group();
		group.setGroupName(model.getGroupName());
		groupDao.save(group);
		return group.getGroupId().toString();
	}

	/**
	 * Title:delGroup
	 * Description:删除组
	 * @param groupId 传入组序号groupId
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String delGroup(Integer groupId) throws Exception{
		Group g = (Group)groupDao.findById(groupId);
		if(null == g)
			throw new ServiceException("ERR-0017");
		groupDao.delete(g);
		return "1";
	}
	
	/**
	 * * Title:addGroupAuthorities
	 * Description: 添加组权限
	 * @param groupId 组序号
	 * @param authorities 权限列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addGroupAuthorities(Integer groupId, String authorities) throws Exception{
		if (null == groupId)
			throw new ServiceException("ERR-0009");
		Group g = null;
		g = (Group) groupDao.findById(groupId);
		if (null == g)
			throw new ServiceException("ERR-0017");
		if(StringUtils.isEmpty(authorities))
			return "1";
		String[] auths = authorities.split(",");
		int len = auths.length;
		GroupAuthority ga = null;
		for (int i = 0; i < len; i++) {
			Authority a = null;
			a = (Authority) authDao.findById(auths[i]);
			ga = new GroupAuthority();
			ga.setGroup(g);
			ga.setAuthority(a);
			gaDao.save(ga);
		}
		return "1";
	}

	/**
	 * Title:delGroupAuthority
	 * Description: 删除组权限
	 * @param groupId 组序号
	 * @param authorities 权限列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String delGroupAuthority(Integer groupId, String authorities) throws Exception{
		if (null == groupId || StringUtils.isEmpty(authorities))
			throw new ServiceException("ERR-0009");
		Group g = null;
		g = (Group) groupDao.findById(groupId);
		if (null == g)
			throw new ServiceException("ERR-0017");
		String[] auths = authorities.split(",");
		int len = auths.length;
		Map map = new HashMap();
		for (int i = 0; i < len; i++) {
			Authority a = null;
			a = (Authority) authDao.findById(auths[i]);
			if (null != a) {
				map.clear();
				map.put("group", g);
				map.put("authority", a);
				gaDao.deleteByProperties(map);
			}
		}
		return "1";
	}

	/**
	 * Title:delGroupAllAuthority
	 * Description: 删除组所有权限
	 * @param groupId 组序号
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String delGroupAllAuthority(Integer groupId) throws Exception{
		Group g = null;
		g = (Group) groupDao.findById(groupId);
		if (null == g)
			throw new ServiceException("ERR-0017");
		Map map = new HashMap();
		map.put("group", g);
		gaDao.deleteByProperties(map);
		return "1";
	}

	/**
	 * Title:updateGroupAuthority
	 * Description: 更新组权限
	 * @param groupId 组序号
	 * @param authorities 权限列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String updateGroupAuthority(Integer groupId, String authorities) throws Exception{
		String m = delGroupAllAuthority(groupId);
		if (!"1".equals(m))
			return m;
		return addGroupAuthorities(groupId, authorities);
	}

	/**
	 * Title:updateAllGroupAuthority
	 * Description: 更新所有组权限
	 * @param groupAuthority 组权限 格式为{"group1":"auth1,auth2,auth3,...",...}
	 * @return 成功 1 失败0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String updateAllGroupAuthority(String groupAuthority) throws Exception{
		JSONObject jobj = null;
		jobj = JSONObject.fromObject(groupAuthority);
		if (null == jobj)
			throw new ServiceException("ERR-0010");
		gaDao.deleteAll();
		Set set = jobj.keySet();
		int result = 1;
		for (Iterator l = set.iterator(); l.hasNext();) {
			Integer groupId = Integer.parseInt(l.next().toString());
			String auths = (String)jobj.get(groupId.toString());
			addGroupAuthorities(groupId, auths);
		}
		return "1";
	}

	/************************* 权限 ********************************/
	/**
	 * Title:addAuthority
	 * Description: 添加权限
	 * @param model Authority对象
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addAuthority(Authority model) throws Exception{
		if(null == model || null == model.getAuthority())
			throw new ServiceException("ERR-0009");
		Authority a = new Authority(model.getAuthority());
		authDao.save(a);
		return model.getAuthority();
	}

	/**
	 * Title:delAuthority
	 * Description: 删除权限
	 * @param model MAuthority对象
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String delAuthority(String ids) throws Exception{
		String[] idS = ids.split(",");
		for (int i=0; i<idS.length; i++) {
			if (null != idS[i]) {
				Authority a = (Authority)authDao.findById(idS[i]);
				if(null == a)
					throw new ServiceException("ERR-0016");
				authDao.delete(a);
			}
		}
		return "1";
	}

	/***************************** 用户分组 ******************/
	/**
	 * Title:addUserToGroups
	 * Description: 添加用户到组
	 * @param uname 用户名
	 * @param groups 组名列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addUserToGroups(Long userId, String groups) throws Exception{
		if (null == userId || StringUtils.isEmpty(groups))
			return "1";
		User u = (User) userDao.findById(userId);
		if (null == u)
			throw new ServiceException("ERR-0011");
		String[] gps = groups.split(",");
		HashMap map = new HashMap<String, Object>();
		int len = gps.length;
		for (int i = 0; i < len; i++) {
			map.clear();
			Group g = (Group) groupDao.findById(Integer.parseInt(gps[i]));
			map.put("user", u);
			map.put("group", g);
			List lst = gmDao.findByProperties(map);
			if (null == lst || 0 == lst.size()) {
				GroupMember gm = new GroupMember();
				gm.setGroup(g);
				gm.setUser(u);
				gmDao.save(gm);
			}
		}
		return "1";
	}

	/**
	 * Title:addUsersToGroup
	 * Description:添加用户到组
	 * @param users 用户名列表
	 * @param groupId 组号
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addUsersToGroup(String users, Integer groupId) throws Exception{
		if (StringUtils.isEmpty(users) || null == groupId)
			throw new ServiceException("ERR-0009");
		Group g = (Group) groupDao.findById(groupId);
		if (null == g)
			throw new ServiceException("ERR-0017");
		String[] us = users.split(",");
		
		for (String u:us) {
			List<User> lstU = userDao.findByProperties(new String[]{"userName"}, new Object[]{u});
			if(null == lstU || lstU.size()==0)
				continue;
			User user = lstU.get(0);
			
			List<GroupMember> lst = gmDao.findByProperties(new String[]{"group","user"},new Object[]{g,u});
			
			if (null == lst || 0 == lst.size()) {
				GroupMember gm = new GroupMember();
				gm.setGroup(g);
				gm.setUser(user);
				gmDao.save(gm);
			}
		}
		return "1";

	}
	
	/**
	 * Title:addUserToGroup
	 * Description:添加用户到组
	 * @param userName 用户名
	 * @param groupName 组号
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String addUserToGroup(Long userId, String groupName) throws Exception{
		if (null == userId || null == groupName)
			throw new ServiceException("ERR-0009");

		List glst = groupDao.findByProperties(new String[]{"groupName"},new Object[]{groupName});
		if(null == glst || glst.size() == 0)
			return "ERR-0017";
		Group g = (Group) glst.get(0);
		if (null == g)
			return "ERR-0017";
		HashMap map = new HashMap<String, Object>();
			
		User u = (User) userDao.findById(userId);
		map.put("user", u);
		map.put("group", g);
		List lst = gmDao.findByProperties(map);
		if (null == lst || 0 == lst.size()) {
			GroupMember gm = new GroupMember();
			gm.setGroup(g);
			gm.setUser(u);
			gmDao.save(gm);
		}
		return "1";
	}
	
	/**
	 * Title:removeUserFromGroups
	 * Description:从组删除用户
	 * @param uname 用户名
	 * @param groups 组名列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String removeUserFromGroups(Integer userId, String groups) throws Exception{
		if (null == userId || null == groups)
			throw new ServiceException("ERR-0009");
		User u = null;
		u = (User) userDao.findById(userId);
		if (null == u)
			throw new ServiceException("ERR-0011");
		String[] gps = groups.split(",");
		int len = gps.length;
		Map map = new HashMap();
		for (int i = 0; i < len; i++) {
			Group g = null;
			g = (Group) groupDao.findById(Integer.parseInt(gps[i]));
			map.clear();
			map.put("user", u);
			map.put("group", g);
			gmDao.deleteByProperties(map);
		}
		return "1";
	}

	/**
	 * Title:removeUsersFromGroup
	 * Description:从组删除用户
	 * @param uname 用户名 
	 * @param groups 组名列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String removeUsersFromGroup(String users, Integer groupId) throws Exception{
		if (null == users || null == groupId)
			throw new ServiceException("ERR-0009");
		Group g = null;
		g = (Group) groupDao.findById(groupId);
		if (null == g)
			throw new ServiceException("ERR-0017");
		String[] us = users.split(",");
		List<String> ul = new ArrayList<String>();
		for(String s:us){
			ul.add(s);
		}
		gmDao.deleteByProperties(new String[]{"user.userName","group.groupId"},new Object[]{ul,groupId},new String[]{"in","="});
		return "1";
	}
	
	/**
	 * Title:removeUserGroups
	 * Description:删除用户所有组
	 * @param uname 用户名
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String removeUserGroups(Integer userId) throws Exception{
		if (null == userId)
			throw new ServiceException("ERR-0009");
		User u = null;
		u = (User) userDao.findById(userId);
		if (null == u)
			throw new ServiceException("ERR-0011");
		Map map = new HashMap();
		map.put("user", u);
		gmDao.deleteByProperties(map);
		return "1";
	}
	
	
	/**
	 * Title:updateUserGroups
	 * Description:更新用户组
	 * @param uname 用户名 
	 * @param groups 组名列表
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String updateUserGroups(String userName, String groups) throws Exception{
		List<User> lstU = userDao.findByProperties(new String[]{"userName"},new Object[]{userName});
		User u = null;
		if(null != lstU && lstU.size()>0)
			u = lstU.get(0);
		if(null == u)
			throw new ServiceException("ERR-0011");
		Map map = new HashMap();
		map.put("user", u);
		gmDao.deleteByProperties(map);
		return addUserToGroups(u.getUserId(), groups);
	}
	
	/**
	 * Title:updateGroupUsers
	 * Description:更新组用户
	 * @param users 用户列表 
	 * @param groupId 组编号
	 * @return 成功 1 失败 0
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String updateGroupUsers(String users, Integer groupId) throws Exception{
		Group g = (Group)groupDao.findById(groupId);
		if(null == g)
			throw new ServiceException("ERR-0017");
		Map map = new HashMap();
		map.put("group", g);
		gmDao.deleteByProperties(map);
		if(null != users)
			return addUsersToGroup(users, groupId);
		return "1";
	}
	
	/**
	 * Title:getUserGroups
	 * Description:获取用户组
	 * @param uname 用户名
	 * @return 成功   [{groupId,groupName},...]
	 * 			失败或没有 null
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getUserGroups(Long userId) throws Exception{
		User user = null;
		user = (User)userDao.findById(userId);
		if(null == user)
			throw new ServiceException("ERR-0011");
		Set<GroupMember> lst = user.getGroupMembers();
		JSONArray jarr = new JSONArray();
		if(lst != null && lst.size() > 0){
			for(Iterator l = lst.iterator();l.hasNext();){
				JSONObject jobj = new JSONObject();
				GroupMember gm = (GroupMember)l.next();
				Group g = gm.getGroup();
				jobj.put("groupId", g.getGroupId());
				jobj.put("groupName", g.getGroupName());
				jarr.add(jobj);
			}
		}
		JSONObject jret = new JSONObject();
		jret.put("rows", jarr);
		return jret.toString();	
	}
	
	/**
	 * Title:getUserGroups
	 * Description:获取用户组列表
	 * @uname：用户名
	 * @return group list
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public List<Group> getUserGroupList(Integer userId) throws Exception{
		User user = null;
		user = (User)userDao.findById(userId);
		if(null == user)
			throw new ServiceException("ERR-0011");
		Set<GroupMember> set = user.getGroupMembers();
		List<Group> lst = new ArrayList<Group>();
		if(set != null && set.size() > 0){
			for(Iterator<GroupMember> l = set.iterator();l.hasNext();){
				lst.add(l.next().getGroup());
			}
		}
		return lst;
	}
	
	/**
	 * Title:getAllAuthorities
	 * Description:获取所有权限
	 * @return 成功 {rows:[]}权限json串 authority
	 * 失败 null
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getAllAuthorities() throws Exception{
		List lst = null;
		lst = authDao.findAll("order by model.authority");
		
		JSONObject jret = new JSONObject();
		JSONArray jarr = new JSONArray();
		if (null != lst) {
			for (Iterator l = lst.iterator(); l.hasNext();) {
				Authority auth = (Authority) l.next();
				JSONObject jobj = new JSONObject();
				jobj.put("authority", auth.getAuthority());
				jarr.add(jobj);
			}
			jret.put("rows", jarr);
		}
		return jret.toString();
	}

	/**
	 * Title:getAllGroups
	 * Description:获取所有组
	 * @return 成功 {rows:[{}]}权限json串 group
	 * 失败 null
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getAllGroups() throws Exception{
		List lst = groupDao.findAll("order by model.groupName");
		JSONArray jarr = new JSONArray();
		JSONObject jret = new JSONObject();
		if (null != lst) {
			for (Iterator l = lst.iterator(); l.hasNext();) {
				Group group = (Group) l.next();
				JSONObject jobj = new JSONObject();
				jobj.put("groupId", group.getGroupId());
				jobj.put("groupName", group.getGroupName());
				jarr.add(jobj);
			}
			jret.put("rows", jarr);
			return jret.toString();
		}
		return jret.toString();
	}

	/**
	 * Title:getAllGroupsWithAuthority
	 * Description:获取所有组并带权限
	 * @return 权限json串 [{groupName:[auth1,auth2,...]},......]
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getAllGroupsWithAuthority() {
		List lst = null;
		lst = groupDao.findAll(null);
		JSONObject jobj = new JSONObject();
		
		if (null != lst) {
			for (Iterator l = lst.iterator(); l.hasNext();) {
				Group group = (Group) l.next();
				List<GroupAuthority> lst1 = gaDao.findByProperties(new String[]{"group.groupId"},new Object[]{group.getGroupId()});
				
				ArrayList<String> arrAuth = new ArrayList<String>();
				//处理组权限
				if (null != lst1){
					for (Iterator<GroupAuthority> it = (Iterator<GroupAuthority>) lst1
							.iterator(); it.hasNext();) {
						GroupAuthority ga = it.next();
						if (null != ga) {
							Authority auth = ga.getAuthority();
							if (null != auth) {
								arrAuth.add("'" + auth.getAuthority() + "'");
							}
						}
					}
				}
				jobj.put(group.getGroupName(), arrAuth.toString());
			}
		}
		JSONObject job = new JSONObject();
		job.put("rows", jobj);
		return job.toString();
	}
	
	/**
	 * Title:getGroupUsers
	 * Description:获取组用户
	 * @param groupId 组序号
	 * @return 用户json串 
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getGroupUsers(Integer groupId) throws Exception{
		Group group = null;
		JSONArray jarr = new JSONArray();
		group = (Group)groupDao.findById(groupId);
		if(null == group)
			throw new ServiceException("ERR-0017");
		Set lst = group.getGroupMembers();
		ArrayList<String> arr = new ArrayList<String>();
		//处理组用户
		if (null != lst){
			for (Iterator<GroupMember> it = (Iterator<GroupMember>) lst.iterator(); it.hasNext();) {
				GroupMember gm = it.next();
				if (null != gm) {
					User u = gm.getUser();
					if (null != u) {
						JSONObject jobj = new JSONObject();
						jobj.put("userName", u.getUserName());
						jobj.put("enable", u.getEnabled());
//						jobj.put("mobile", u.getMobile());
						jarr.add(jobj);
					}
				}
			}
		}
		JSONObject job = new JSONObject();
		job.put("rows", jarr);
		return job.toString();
	}
	
	/**
	 * Title:getGroupUsersDetail
	 * Description:获取组用户(带细节)
	 * @param groupId 组序号
	 * @param pageno 页面编号
	 * @param pagesize 页面所含记录的条数
	 * @return 用户json串 
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getGroupUsersDetail(Integer groupId,int pageno,int pagesize) throws Exception{
		Group group = null;
		group = (Group)groupDao.findById(groupId);
		if(null == group)
			throw new ServiceException("ERR-0017");
		long total = 0;
		int start = (pageno - 1) * pagesize;
		int max = pagesize;
		Map map = new HashMap();
		map.put("group", group);
		if(1 == pageno)
			total = gmDao.getPropertiesCount(map, null, "order by model.user.userName");
		List lst = gmDao.findByPropertiesRule(map, null, "order by model.user.userName", start, max);
		JSONArray jarr = new JSONArray();
		//处理组用户
		if (null != lst){
			for (Iterator<GroupMember> it = (Iterator<GroupMember>) lst
					.iterator(); it.hasNext();) {
				GroupMember gm = it.next();
				if (null != gm) {
					User u = gm.getUser();
					String uname = u.getUserName();
					JSONObject jobj = new JSONObject();
					jobj.put("userName", uname);
					
					jobj.put("enable", u.getEnabled());
					jobj.put("userName", u.getUserName());
					jobj.put("enable", u.getEnabled());
//					jobj.put("mobile", u.getMobile());
					jarr.add(jobj);
				}
			}
		}
		JSONObject jret = new JSONObject();
		if(0 != total)
			jret.put("total", total);
		jret.put("rows", jarr);
		return jret.toString();
	}
	/**
	 * Title:getAuthsByGroup
	 * Description:获取组权限
	 * @param groupId:组编号
	 * @return 组权限json串
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public String getAuthsByGroup(Integer groupId) throws Exception{

		Group group = null;
		group = (Group)groupDao.findById(groupId);
		if(null == group)
			return "ERR-0017";
		List<GroupAuthority> lstAuth = gaDao.findByProperties(new String[]{"group.groupId"},new Object[]{groupId});
	
		JSONObject jret = new JSONObject();
		JSONArray jarr = new JSONArray();
		
		if(null!=lstAuth){
			for (GroupAuthority gah:lstAuth) {

				JSONObject jobj = new JSONObject();
				jobj.put("authority", gah.getAuthority().getAuthority());
				jarr.add(jobj);
			}
		}
		jret.put("rows", jarr);
		return jret.toString();
	}
	
	/**
	 * Title:getUserAuthorities
	 * Description:获取用户权限
	 * @param userName:用户名
	 * @return 成功：权限数组，失败：错误编号
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public ArrayList getUserAuthorities(Integer userId) throws Exception{
		User user = (User)userDao.findById(userId);
		if(null == user)
			return null;
		List lst = null;
    	lst = authDao.getAuthorityByUser(user);
		ArrayList<String> arr = new ArrayList<String>();
		if(null != lst){
			for(Iterator<Authority>ite = lst.iterator();ite.hasNext();){
				arr.add(ite.next().getAuthority());
			}
		}
		return arr;
	}
	
	
	/**
	 * Title:getAuthorityUsers
	 * Description:检查用户是否有权限
	 * @param userName:用户名
	 * @param auth:权限名
	 * @return 成功：true, 失败 false
	 * @exception 处理异常，抛出Exception交由上层异常处理层进行处理
	 */
	public boolean isUserHasAuthority(Integer userId,String auth) throws Exception{
		ArrayList<String> arr = getUserAuthorities(userId);
		if(null != arr){
			int l1 = arr.size();
			for(int i=0;i<l1;i++){
				if(arr.get(i).equals(auth))
					return true;
			}
		}
		return false;
	}

	public String updateUserGroups(Long userId, String ids) throws Exception {
		List<User> lstU = userDao.findByProperties(new String[]{"userId"},new Object[]{userId});
		User u = null;
		if(null != lstU && lstU.size()>0)
			u = lstU.get(0);
		if(null == u)
			throw new ServiceException("ERR-0011");
		Map map = new HashMap();
		map.put("user", u);
		gmDao.deleteByProperties(map);
		return addUserToGroups(u.getUserId(), ids);
	}

	
}
